package ui;

import java.awt.Dimension;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.JTable;

import core.BrowserLauncher;
import core.Library;

public class GameContextMenu extends JPopupMenu {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JMenuItem jMenuItemBGG = null;
	private JMenuItem jMenuItemGoogle = null;
	private ImagePanel imagepanel = null;
	private JTable m_table = null;
	private String m_googleword = null;  //  @jve:decl-index=0:
	private JMenuItem jMenuItemPGDB = null;
	public void setGoogleWord(String str){
		m_googleword = str;
	}

	public GameContextMenu() {
		super();
		initialize();
		// TODO 自動生成されたコンストラクター・スタブ
	}


	public GameContextMenu(String label) {
		super(label);
		initialize();
		// TODO 自動生成されたコンストラクター・スタブ
	}
	
	public GameContextMenu(JTable table){
		super();
		m_table = table;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.add(getJMenuItemBGG());
		if (m_table != null){
			this.add(getJMenuItemPGDB());
			this.add(getJMenuItemGoogle());
			this.add(getImagePanel());
		}
	}
	
	private ImagePanel getImagePanel(){
		if (imagepanel == null){
			String[] imagepath = new String[1];
			imagepath[0] =  Library.HOMEDIR + "imagecache/" + findTargetGame("id",m_table) + ".jpg";
			imagepanel = new ImagePanel(imagepath, true);
			Icon icon = new ImageIcon(getToolkit().createImage(imagepath[0]));
			imagepanel.setPreferredSize(new Dimension(icon.getIconWidth(),icon.getIconHeight()));
		}
		return imagepanel;
	}
	/**
	 * This method initializes jMenuItemBGG	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemBGG() {
		if (jMenuItemBGG == null) {
			jMenuItemBGG = new JMenuItem();
			jMenuItemBGG.setText("BGG");
			jMenuItemBGG.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String url = "http://www.boardgamegeek.com/game/" + findTargetGame("id",m_table);
					BrowserLauncher.openURL(url);
				}
			});
		}
		return jMenuItemBGG;
	}
	/**
	 * This method initializes jMenuItemGoogle	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemGoogle() {
		if (jMenuItemGoogle == null) {
			jMenuItemGoogle = new JMenuItem();
			jMenuItemGoogle.setText("Google");
			jMenuItemGoogle.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					String url;
					try {
						url = "http://www.google.com/search?q=" + URLEncoder.encode(findTargetGame("Title",m_table), "UTF-8") + "+" + m_googleword;
						BrowserLauncher.openURL(url);
					} catch (UnsupportedEncodingException e1) {
						// TODO 自動生成された catch ブロック
						e1.printStackTrace();
					}
				}
			});
		}
		return jMenuItemGoogle;
	}
	
	private String findTargetGame(String key, JTable table){
		int index;
		for (index=0; index<table.getColumnCount(); index++){
			if (table.getColumnName(index).equals(key)){
				break;
			}
		}
		if (index == table.getColumnCount()){
			return null;
		}
		return table.getValueAt(table.getSelectedRow(),index).toString();
	}

	/**
	 * This method initializes jMenuItemPGDB	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemPGDB() {
		if (jMenuItemPGDB == null) {
			jMenuItemPGDB = new JMenuItem();
			jMenuItemPGDB.setText("play:game");
			jMenuItemPGDB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					String[] strAry = findTargetGame("Title",m_table).split("[ \\.,;:-?]");
					int max_size = 0;
					int marked_index = 0;
					int i=0;
					for (i=0; i<strAry.length; i++){
						if ( strAry[i].trim().length() >= max_size && strAry[i].trim().indexOf("xpansion") == -1 
								&& strAry[i].trim().indexOf("artenspiel") == -1
								&& strAry[i].trim().indexOf("xtension") == -1){
							max_size = strAry[i].trim().length();
							marked_index = i;
						}
					}
					System.out.println(strAry[marked_index].trim());
					//http://www.gamers-jp.com/playgame/db_searchlist.php?search_str=
					String url2 = "http://www.gamers-jp.com/playgame/db_searchlist.php?search_str=" + strAry[marked_index].trim();
					BrowserLauncher.openURL(url2);
				}
			});
		}
		return jMenuItemPGDB;
	}

}

class DataComparator implements java.util.Comparator{
	  public int compare(Object o1, Object o2){
	    return ((String)o2).length() - ((String)o1).length();
	  }
}
